package com.cris.controller;

import com.cris.domain.*;
import com.cris.service.OrderService;
import com.cris.view.TableModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by owen on 2017/4/21.
 */
@Controller
public class SaleController {
    @Autowired
    OrderService orderService;

    Store store;
    Register register;
    private List<TableModel> tableModels = new ArrayList<TableModel>();
    @RequestMapping(value = "/sale",method = RequestMethod.GET)
    public void sale(){
        store = new Store();
        register =store.getRegister();
        register.makeNewSale();
        orderService.addOrderNo(register.getCurrentSale().getOrderNo());
        tableModels.removeAll(tableModels);
    }

    @RequestMapping(value = "/enter_item",method = RequestMethod.POST)
    public String enterItem(@RequestParam String code, @RequestParam String quantities, Model model){
        register = store.getRegister();
        int id = Integer.parseInt(code);
        int quantity = Integer.parseInt(quantities);
        ItemId itemId = register.getCatalog().getItemId(id);
        String description = register.getCatalog().getProductDescription(itemId).getDescription();
        double price = orderService.getPrice(register,itemId);
        register.enterItem(itemId, quantity);
        String orderNo = orderService.getOrderNo(register);
        TableModel vo = new TableModel(id,description,price,quantity);
        tableModels.add(vo);
        orderService.addlineItems(id,quantity,price,orderNo);
        model.addAttribute(tableModels);
        return "sale";
    }

    @RequestMapping(value = "/end_sale",method = RequestMethod.GET)
    public String endSale(Model model){
        register = store.getRegister();
        double total = register.getCurrentSale().getTotal().getAmount();
        register.endSale();
        orderService.addOrder(register.getCurrentSale().getOrderNo(),total);
        model.addAttribute("total",Double.toString(total));
        model.addAttribute(tableModels);
        return "sale";
    }

    @RequestMapping(value = "/pay",method = RequestMethod.POST )
    public String makePayment(@RequestParam String amount,Model model){
        register = store.getRegister();
        double amounts = Double.parseDouble(amount);
        register.makePayment(new Money(amounts));
        double balance = register.getCurrentSale().getBalance().getAmount();
        if (balance>0){
            model.addAttribute("balance",balance);
        }
        else {
            boolean flag =true;
            model.addAttribute("flag",flag);
        }
        String payNo = register.getCurrentSale().getPayment().getPayNo();

        double total = register.getCurrentSale().getTotal().getAmount();
        double recieve = amounts;
        System.out.println(total);
        String orderNo = orderService.getOrderNo(register);
        if (balance>0){
        orderService.addPayment(total,payNo,orderNo);}

        model.addAttribute("total",total);
        model.addAttribute("recieve",recieve);
        model.addAttribute(tableModels);
        return "sale";
    }
}
